<template>
  <div class="bigbox">
    <!-- 表头盒子 -->
    <!-- 盒子主体 -->
    <div class="mainbox">
      <div class="top">
        <div class="left">
          <div class="group-select">
            <el-select v-model="groupId" placeholder="选择所属分组">
              <el-option
                v-for="item in groupList"
                :key="item.value"
                :label="item.text"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
          <div class="group-add" @click="groupOpen">
            <i class="el-icon-plus"></i>
          </div>
          <div class="search">
            <el-input
              placeholder="请输入关键字搜索"
              prefix-icon="el-icon-search"
              v-model="inputValue"
            >
            </el-input>
          </div>
        </div>
        <div class="towBtn">
          <button @click="chongzhi" class="button">重置</button>
          <button @click="chaxun" class="button chaxun">查询</button>
        </div>
      </div>
      <div class="bodybox">
        <!-- 新增按钮 -->
        <!-- 循环图片盒子 -->
        <div class="table">
          <div class="titlebox">
            <div class="text"></div>
            <div
              class="right"
              :style="{ alignItems: 'center', lineHeight: '32.5px' }"
            >
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="submit('add')"
              >
                <i style="margin-left: 0px" class="el-icon-plus"></i>新增
              </button>
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="dialogOpen('enable')"
              >
                启用
              </button>
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="dialogOpen('inherit')"
              >
                继承
              </button>
              <el-tooltip
                class="item"
                effect="dark"
                content="刷新"
                placement="top"
              >
                <span
                  @click="shuaxin"
                  :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-refresh-right"></i
                ></span>
              </el-tooltip>
              <el-dropdown trigger="click" @command="command">
                <span class="el-dropdown-link">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="密度"
                    placement="top"
                  >
                    <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                      ><i class="el-icon-d-caret"></i
                    ></span>
                  </el-tooltip>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item class="dropdown-list" command="默认"
                    >默认</el-dropdown-item
                  >
                  <el-dropdown-item class="dropdown-list" command="中等"
                    >中等</el-dropdown-item
                  >
                  <el-dropdown-item class="dropdown-list" command="紧凑"
                    >紧凑</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%;"
            height="400px"
            border
            id="exportTabyong"
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center',
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <el-table-column align="left" prop="groupName" label="所属分组">
            </el-table-column>
            <el-table-column align="left" prop="propertyCode" label="属性编码">
            </el-table-column>
            <el-table-column
              align="left"
              prop="classPropertyName"
              label="属性名称"
            >
            </el-table-column>
            <el-table-column align="left" prop="propertyType" label="属性类型">
              <template slot-scope="scope">
                <span>{{
                  propTypeList.find((i) => i.value === scope.row.propertyType)
                    ?.text
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              prop="extendProperty"
              label="继承属性"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.extendProperty ? "是" : "否" }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              prop="propertyClassName"
              label="所属类名称"
            >
            </el-table-column>
            <el-table-column align="left" prop="propertyUnits" label="计量单位">
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <div class="table-cz">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="编辑"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-edit"
                      @click="edit(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-delete"
                      @click="del(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="上移"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-upload2"
                      @click="move('up', scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="下移"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-download"
                      @click="move('down', scope.row)"
                    ></i
                  ></el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <!-- <el-pagination
            background
            class="pagination"
            layout="prev, pager, next"
            :current-page.sync="pageobj._pageNo"
            @current-change="chaxun"
            :total="total"
          >
          </el-pagination> -->
        </div>
        <!-- 新增/修改弹框 -->
        <el-dialog
          :show-close="true"
          :visible.sync="dialogVisible"
          width="500px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <div class="dialog">
            <!-- 企业下拉 -->
            <el-form
              ref="dialogForm"
              :rules="rules"
              :model="dialogForm"
              label-width="110px"
            >
              <el-form-item label="属性编码" prop="propCode">
                <el-input
                  v-model="dialogForm.propCode"
                  :disabled="isedit === 'edit'"
                ></el-input>
              </el-form-item>
              <el-form-item label="属性名称" prop="propName">
                <el-input
                  v-model="dialogForm.propName"
                  :disabled="isedit === 'edit'"
                ></el-input>
              </el-form-item>
              <el-form-item label="属性类型" prop="propType">
                <el-select v-model="dialogForm.propType" placeholder="请选择">
                  <el-option
                    v-for="item in propTypeList"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="所属分组" prop="groupId">
                <el-select v-model="dialogForm.groupId" placeholder="请选择">
                  <el-option
                    v-for="item in groupList"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="默认值" prop="defaultValue">
                <el-input v-model="dialogForm.defaultValue"></el-input>
              </el-form-item>
              <el-form-item label="固定值" prop="fixedValue">
                <el-input v-model="dialogForm.fixedValue"></el-input>
              </el-form-item>
              <el-form-item label="计量单位" prop="propUnits">
                <el-input v-model="dialogForm.propUnits"></el-input>
              </el-form-item>
              <el-form-item label="属性说明" prop="propExplain">
                <el-input
                  v-model="dialogForm.propExplain"
                  type="textarea"
                  :rows="4"
                  resize="none"
                ></el-input>
              </el-form-item>
            </el-form>
            <!-- 弹窗内容 -->
          </div>
          <div style="margin-top: 10px; text-align: center">
            <span slot="footer" class="dialog-footer">
              <el-button @click="diaClose">取 消</el-button>
              <el-button type="primary" @click="add">保 存</el-button>
            </span>
          </div>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="tableVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <TableUse
            v-loading="loading2"
            :data="tableUseData"
            border
            ref="table"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center',
            }"
            :tableConfig="tableConfig"
            :name="this.status === 'enable' ? 'classPropertyId' : 'propertyId'"
            @selection-change="chooseList"
          >
            <template v-slot:propertyType="row">
              {{
                propTypeList.find((i) => i.value === row.row.propertyType)?.text
              }}
            </template>
            <template v-slot:extendProperty="row">
              <span>{{ row.row.extendProperty ? "是" : "否" }}</span>
            </template>
          </TableUse>
          <el-form
            ref="tableForm"
            :rules="tableRules"
            :model="form"
            label-width="80px"
          >
            <el-form-item style="width: 300px" label="所属分组" prop="groupId">
              <el-select v-model="form.groupId">
                <el-option
                  v-for="item in groupList"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div style="margin-top: 20px; text-align: center">
            <span slot="footer" class="dialog-footer">
              <el-button @click="diaClose">取 消</el-button>
              <el-button type="primary" @click="tableSubmit">确 定</el-button>
            </span>
          </div>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="groupVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <GroupUse
            :classId="String(this.classData.classId)"
            :groupUseData="groupUseData"
            @groupUpdata="groupUpdata"
          ></GroupUse>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  addClassPropertyApi,
  delPClassropertyApi,
  downClassPropertApi,
  editClassPropertyApi,
  enablePropertApi,
  extendPropertApi,
  getClassListApi,
  getClassPropertApi,
  getExtendPropertApi,
  getGroupListApi,
  getGroupSelectApi,
  getPropTypeListApi,
  getUnusedPropertApi,
  upClassPropertApi
} from '@/api/Device-all/Device-model'
import TableUse from '../../components/TableUse.vue'
import GroupUse from './GroupUse.vue'
export default {
  name: 'DeviceClassMaintain',
  components: { TableUse, GroupUse },
  props: {
    classData: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      groupId: '',
      filterText: '',
      inputValue: '',
      tableData: [],
      tableUseData: [],
      groupUseData: [],
      loading: false,
      loading2: false,
      iissize: 'medium',
      tableRules: {
        groupId: [
          { required: true, message: '请选择所属分组', trigger: 'change' }
        ]
      },
      dialogForm: {},
      form: {},
      propTypeList: [],
      status: 'enable',
      rules: {
        propCode: [
          { required: true, message: '请输入属性编码', trigger: 'blur' }
        ],
        propName: [
          { required: true, message: '请输入属性名称', trigger: 'blur' }
        ],
        propType: [
          { required: true, message: '请选择属性类型', trigger: 'change' }
        ],
        groupId: [
          { required: true, message: '请选择所属分组', trigger: 'change' }
        ]
      },
      groupList: [],
      deflist: [],
      dialogVisible: false,
      tableVisible: false,
      groupVisible: false,
      isedit: 'add'
      // pageobj: {
      //   _pageNo: 0,
      //   _pageSize: 15,
      // },
      // total: 0,
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  },
  computed: {
    tableConfig () {
      if (this.status === 'enable') {
        return [
          {
            align: 'left',
            prop: 'propertyCode',
            label: '属性编码'
          },
          {
            align: 'left',
            prop: 'classPropertyName',
            label: '属性名称'
          },
          {
            align: 'left',
            prop: 'propertyType',
            label: '属性类型'
          },
          {
            align: 'left',
            prop: 'extendProperty',
            label: '继承属性'
          },
          {
            align: 'left',
            prop: 'propertyClassName',
            label: '所属类名称'
          },
          {
            align: 'left',
            prop: 'propertyUnits',
            label: '计量单位'
          }
        ]
      } else {
        return [
          {
            align: 'left',
            prop: 'propertyCode',
            label: '属性编码'
          },
          {
            align: 'left',
            prop: 'propertyName',
            label: '属性名称'
          },
          {
            align: 'left',
            prop: 'propertyType',
            label: '属性类型'
          },
          {
            align: 'left',
            prop: 'propertyClassName',
            label: '所属类名称'
          }
        ]
      }
    }
  },
  created () {
    this.getGroupSelectList(this.classData.classId)
    this.getClassList()
    this.getPropTypeList()
    this.getGroupList()
  },
  methods: {
    // 获取分组下拉
    async getGroupSelectList (id) {
      const res = await getGroupSelectApi(id)
      this.groupList = res.data.list
    },
    // 获取类属性列表
    async getClassList () {
      this.loading = true
      const res = await getClassListApi({
        classId: this.classData.classId,
        groupId: this.groupId
      })
      this.deflist = res.data.list
      this.tableData = res.data.list
      this.loading = false
      console.log(res)
    },
    // 获取属性类型
    async getPropTypeList () {
      const res = await getPropTypeListApi()
      this.propTypeList = res.data.list
    },
    // 新增弹窗
    submit (val) {
      this.isedit = val
      this.dialogForm.classId = this.classData.classId
      this.dialogVisible = true
    },
    // 启用/继承弹窗打开
    async dialogOpen (type) {
      this.status = type
      this.$set(this.form, 'groupId', this.groupList[0].value)
      this.perpertList()
      this.tableVisible = true
    },
    // 分组维护弹窗打开
    groupOpen () {
      this.groupVisible = true
    },
    // 获取 启用/继承 列表
    async perpertList () {
      const res =
        this.status === 'enable'
          ? await getUnusedPropertApi(this.classData.classId)
          : await getExtendPropertApi(this.classData.classId)
      this.tableUseData = res.data.list
    },
    // 查询自定义属性分组列表
    async getGroupList () {
      const res = await getGroupListApi(this.classData.classId)
      console.log(res)
      this.groupUseData = res.data.list
    },
    // 弹窗关闭
    diaClose () {
      this.dialogVisible = false
      this.tableVisible = false
      this.groupVisible = false
      this.dialogForm = {}
      this.form = {}
    },
    async shuaxin () {
      await this.chaxun()
      this.$message({
        type: 'success',
        message: '列表刷新成功',
        duration: 2000
      })
    },
    async groupUpdata () {
      await this.getGroupList()
      this.getGroupSelectList(this.classData.classId)
    },
    // 查询分页信息
    async chaxun () {
      await this.getClassList()
      const list = [...this.deflist]
      if (this.inputValue) {
        this.tableData = list.filter(
          (item) =>
            item.propertyCode.includes(this.inputValue) ||
            item.classPropertyName.includes(this.inputValue)
        )
      } else {
        this.tableData = [...list]
      }
    },
    command (val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    // 新增保存/修改
    async add () {
      await this.$refs.dialogForm.validate()
      console.log(this.dialogForm)
      if (this.isedit === 'add') {
        const res = await addClassPropertyApi(this.dialogForm)
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: res.message,
            duration: 2000
          })
          this.getClassList()
        }
      } else if (this.isedit === 'edit') {
        const res = await editClassPropertyApi({
          classPropertyId: this.dialogForm.classPropertyId,
          defaultValue: this.dialogForm.defaultValue,
          fixedValue: this.dialogForm.fixedValue,
          groupId: this.dialogForm.groupId,
          propExplain: this.dialogForm.propExplain,
          propName: this.dialogForm.propName,
          propType: this.dialogForm.propType,
          propUnits: this.dialogForm.propUnits
        })
        console.log(res)
        this.$message({
          type: 'success',
          message: res.message,
          duration: 2000
        })
        this.getClassList()
      }
      this.diaClose()
    },
    // 移动
    async move (type, item) {
      this.$confirm('此操作将移动类属性, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res =
            type === 'up'
              ? await upClassPropertApi({
                classPropertyId: item.classPropertyId
              })
              : await downClassPropertApi({
                classPropertyId: item.classPropertyId
              })
          console.log(res)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message,
              duration: 2000
            })
          } else {
            this.$message(res.message)
          }
          this.getClassList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消移动'
          })
        })
    },
    // 编辑
    async edit (item) {
      console.log(item)
      this.isedit = 'edit'
      const res = await getClassPropertApi(item.classPropertyId)
      // console.log(res)
      const data = res.data.entity
      this.dialogForm = {
        classPropertyId: String(data.classPropertyId),
        defaultValue: data.defaultValue,
        fixedValue: data.fixedValue,
        groupId: String(data.groupId),
        propExplain: data.propertyExplain,
        propName: data.classPropertyName,
        propType: String(data.propertyType),
        propUnits: data.propertyUnits,
        propCode: data.propertyCode
      }
      this.$forceUpdate()
      this.dialogVisible = true
    },
    // 删除
    async del (item) {
      this.$confirm('此操作将永久删除该类属性, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await delPClassropertyApi(item.classPropertyId)
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.message,
                duration: 2000
              })
              this.getClassList()
            } else {
              this.$message({
                type: 'error',
                message: res.message,
                duration: 2000
              })
            }
          } catch (e) {
            console.log(e)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    chongzhi () {
      this.inputValue = ''
      this.groupId = ''
      this.getClassList()
    },
    chooseList (arr) {
      console.log(arr)
      if (this.status === 'enable') {
        this.form.classPropertyId = arr
      } else {
        this.form.propertyId = arr
      }
      // this.form.classPropertyId = arr.join(',')
      // this.form.propertyId = arr.join(',')
    },
    // 启用/继承保存
    async tableSubmit () {
      console.log(this.form)
      const res =
        this.status === 'enable'
          ? await enablePropertApi(this.form)
          : await extendPropertApi(this.form)
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: res.data.content,
          duration: 2000
        })
        this.perpertList()
        this.getClassList()
      } else {
        this.$message({
          type: 'error',
          message: res.message,
          duration: 2000
        })
      }
      console.log(res)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep textarea {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
.table-cz {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 40px;
  height: 40px;
  line-height: 40px;
  border: 1px solid #d9d9d9;
  text-align: center;
}
.avatar {
  width: 40px;
  height: 40px;
  display: block;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
::v-deep .el-table{
  box-sizing: border-box;
  padding: 0;
}
.bigbox {
  background-color: #f1f1f1;
  min-height: 100%;
  padding: 9px;
  display: flex;
  .mainbox {
    flex: 1;
    .top {
      padding: 0 20px;
      display: flex;
      justify-content: space-between;
      width: 100%;
      height: 80px;
      background-color: #fff;
      border-bottom: 1px solid #f1f1f1;
      margin-bottom: 20px;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .group-select {
          width: 200px;
          margin-right: 10px;
        }
        .group-add {
          display: flex;
          justify-content: center;
          align-items: center;
          cursor: pointer;
          border: 1px solid #dcdfe6;
          border-radius: 5px;
          width: 25px;
          height: 25px;
          margin-right: 20px;
        }
        .search {
          display: flex;
          align-items: center;
          width: 200px;
        }
      }
    }
    .bodybox {
      position: relative;
      background-color: #fff;
      padding: 20px;
      padding-bottom: 30px;
      .pagination {
        position: absolute;
        right: 20px;
        bottom: 10px;
      }
      .table {
        .titlebox {
          display: flex;
          justify-content: space-between;
          margin-bottom: 10px;
          .text {
            font-size: 16px;
          }
          .right {
            align-items: center;
            i {
              margin-left: 10px;
            }
            .bewdao {
              width: 63px;
              height: 32px;
              margin-right: 20px;
            }
            .daochu {
              background-color: #1890ff;
              color: #fff;
              border: none;
              border-radius: 3px;
            }
          }
        }
      }
      .dialog {
        .title {
          font-size: 28px;
          margin-bottom: 30px;
        }
        .text {
          font-size: 12px;
          color: #3b8eea;
        }
      }
    }
  }
}
::v-deep.el-table th.gutter{
  display: none;
  width:0
}
::v-deep.el-table colgroup col[name='gutter']{
  display: none;
  width: 0;
}
::v-deep.el-table__body{
  width: 100% !important;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-input {
  height: 32px;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 1;
}
.el-select-dropdown__item {
  padding-left: 10px;
}
::v-deep .el-date-editor {
  width: 100%;
}

.el-form-item {
  margin-bottom: 20px;
}
.button {
  width: 63px;
  height: 32px;
  margin-right: 5px;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 3px;
}
.chaxun {
  background-color: #1890ff;
  color: #fff;
}
::v-deep .el-range-separator {
  line-height: 25px;
}
.timeTitle {
  height: 50px;
}

.dropdown-list {
  padding: 0;
  text-align: center;
  width: 50px;
}

.change-type {
  width: 100px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  border-radius: 4px;
  background-color: rgba(255, 255, 255, 1);
  font-size: 14px;
  color: #606266;
  margin-right: 10px;
}

.node-img {
  width: 16px;
  height: 16px;
  margin-right: 5px;
  vertical-align: bottom;
}
</style>
